﻿using CompanyManageSystem.Model;
using CompanyManageSystem.UI.Info;
using CompanyManageSystem.UI.Repast;
using DevExpress.XtraBars;
using DevExpress.XtraNavBar;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Main
{
    public partial class Frm_Mains : Form
    {
        #region 属性
        public Guid ID { get; set; }
        #endregion

        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_Mains()
        {
            InitializeComponent();

            InitSkin();
        }
        public Frm_Mains(Guid id)
        {
            InitializeComponent();
            this.ID = id;

            InitSkin();
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Mains_Load(object sender, EventArgs e)
        {
            barButtonItem10.Caption = GetIPAddress();
            
            InitMenu();
        }

        /// <summary>
        /// 初始化窗口
        /// </summary>
        public void InitMenu()
        {
            PSContext _db = new PSContext();
            this.navBarMenu.Groups.Clear();

            //添加组
            NavBarGroup nbg = new NavBarGroup();
            nbg.Name = "group1";
            nbg.Caption = "管理业务";
            nbg.GroupStyle = NavBarGroupStyle.ControlContainer;
            this.navBarMenu.Groups.Add(nbg);

            //添加控制面板
            NavBarGroupControlContainer nbgcc = new NavBarGroupControlContainer();
            nbg.ControlContainer = nbgcc;

            //添加树
            TreeList tl = new TreeList();
            tl.SelectImageList = this.imageCollection1;

            #region TreeList属性设置
            tl.BackColor = Color.White;
            tl.Dock = DockStyle.Fill;
            tl.LookAndFeel.UseWindowsXPTheme = true;
            tl.LookAndFeel.UseDefaultLookAndFeel = false;
            tl.OptionsSelection.EnableAppearanceFocusedCell = true;
            tl.OptionsSelection.EnableAppearanceFocusedRow = false;
            tl.Appearance.FocusedCell.BackColor = Color.LightSteelBlue;
            tl.Appearance.FocusedCell.BackColor2 = Color.SteelBlue;
            tl.OptionsBehavior.Editable = false;
            tl.OptionsView.ShowColumns = false;
            tl.OptionsView.ShowHorzLines = false;
            tl.OptionsView.ShowIndentAsRowStyle = true;
            tl.OptionsView.ShowIndicator = false;
            tl.OptionsView.ShowVertLines = false;
            tl.OptionsView.ShowFocusedFrame = false;
            tl.OptionsSelection.EnableAppearanceFocusedRow = false;
            tl.MouseClick += MenuTree_Click;
            tl.FocusedNodeChanged += new DevExpress.XtraTreeList.FocusedNodeChangedEventHandler(this.treelist1_FocusedNodeChanged);
            #endregion

            //设置主父键关系
            tl.KeyFieldName = "ID";
            tl.ParentFieldName = "ParentID";

            //添加列
            TreeListColumn tlc1 = tl.Columns.Add();
            tlc1.FieldName = "Name";
            tlc1.Visible = true;

            TreeListColumn tlc2 = tl.Columns.Add();
            tlc2.FieldName = "WinName";
            tlc2.Visible = false;

            //加载数据
            tl.DataSource = _db.BusinessNavs.OrderBy(p => p.WinName).ToList();

            nbgcc.Controls.Add(tl);

        }

        /// <summary>
        /// TreeList焦点行改变事件(未使用)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void treelist1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            //if (!MdiIsActivity("Frm_User"))
            //{
            //Frm_User frm = new Frm_User();
            //frm.TopLevel = false;
            //frm.MdiParent = this;
            //frm.WindowState = FormWindowState.Maximized;
            //frm.Show();
            //}
        }

        /// <summary>
        /// TreeList节点点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuTree_Click(object sender, MouseEventArgs e)
        {
            TreeList treelist = sender as TreeList;
            TreeListHitInfo hit = treelist.CalcHitInfo(new Point(e.X, e.Y));
            if (hit.Node != null)
            {
                TreeListNode node = hit.Node;
                string winName = node.GetDisplayText("WinName");
                if (!string.IsNullOrEmpty(winName))
                {
                    ShowMdiWindow(winName);
                }
            }
        }

        /// <summary>
        /// 子窗口显示方法
        /// </summary>
        public void ShowMdiWindow(string winName)
        {
            if (winName.Equals("Frm_User"))
            {
                if (!MdiIsActivity("Frm_User"))
                {
                    Frm_User frm = new Frm_User();
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
            else if (winName.Equals("Frm_Company"))
            {
                if (!MdiIsActivity("Frm_Company"))
                {
                    Frm_Company frm = new Frm_Company();
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
            else if (winName.Equals("Frm_Supplier"))
            {
                if (!MdiIsActivity("Frm_Supplier"))
                {
                    Frm_Supplier frm = new Frm_Supplier();
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
            else if (winName.Equals("Frm_Staff"))
            {
                if (!MdiIsActivity("Frm_Staff"))
                {
                    Frm_Staff frm = new Frm_Staff();
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
            else if (winName.Equals("Frm_Unit"))
            {
                if (!MdiIsActivity("Frm_Unit"))
                {
                    Frm_Unit frm = new Frm_Unit();
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
            else if (winName.Equals("Frm_PurchaseStorageOrder"))
            {
                if (!MdiIsActivity("Frm_PurchaseStorageOrder"))
                {
                    Frm_PurchaseStorageOrder frm = new Frm_PurchaseStorageOrder();
                    frm.TopLevel = false;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Show();
                }
            }
        }

        /// <summary>
        /// 子窗口选择判断
        /// </summary>
        /// <param name="frmName"></param>
        /// <returns></returns>
        public bool MdiIsActivity(string frmName)
        {
            bool bl = false;
            foreach (Form childFrom in this.MdiChildren)
            {
                //获取子窗口的名称
                if (childFrom.GetType().Name == frmName)
                {
                    //显示子窗口
                    //如果当前窗口已经打开，则显示就行了，不需要创建新的窗口
                    childFrom.Visible = true;
                    childFrom.Activate();
                    bl = true;
                }
            }
            return bl;
        }

        #region 工具栏
        /// <summary>
        /// 工具栏合并
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barManager1_Merge(object sender, BarManagerMergeEventArgs e)
        {
            //子窗口中的BarManager中的Tools工具条
            Bar childBar = e.ChildManager.Bars["Tools"];
            //父窗口中的BarManager中的Tools工具条
            Bar parentBar = barManager1.Bars["Tools2"];
            if (childBar != null)
            {
                //把子窗口中的Tools工具条添加到父窗口中的Tools工具条
                parentBar.Merge(childBar);
            }
        }

        /// <summary>
        /// 工具栏拆分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barManager1_UnMerge(object sender, BarManagerMergeEventArgs e)
        {
            //只显示父窗口中的Tools工具条()
            Bar parentBar = barManager1.Bars["Tools"];
        }
        #endregion

        /// <summary>
        /// 管理员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem9_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frm_Manager frm = new Frm_Manager();
            frm.ShowDialog();
        }

        /// <summary>
        /// 获取本机IP地址
        /// </summary>
        /// <returns></returns>
        private string GetIPAddress()
        {
            string hostName = Dns.GetHostName();                    //获取本机名
            IPHostEntry localHost = Dns.GetHostByName(hostName);    //获取IPv4地址
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            IPAddress localAdd = localHost.AddressList[0];
            return localAdd.ToString();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frm_ModifyPsw frm = new Frm_ModifyPsw(ID);
            frm.ShowDialog();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            //this.Close();
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        #region 工具
        /// <summary>
        /// 计算器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem12_ItemClick(object sender, ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        /// <summary>
        /// 记事本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem13_ItemClick(object sender, ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("notepad.exe");
        }
        #endregion

        /// <summary>
        /// 切换用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            
            
        }

        #region 皮肤
        /// <summary>
        /// 初始化皮肤
        /// </summary>
        private void InitSkin()
        {
            //迭代出所有皮肤样式  
            foreach (DevExpress.Skins.SkinContainer skin in DevExpress.Skins.SkinManager.Default.Skins)
            {
                BarButtonItem barBI = new BarButtonItem();
                barBI.Tag = skin.SkinName;
                barBI.Name = skin.SkinName;
                barBI.Caption = skin.SkinName;
                barBI.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(ItemClick);
                this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] { barBI });
                barSubItem3.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] { new DevExpress.XtraBars.LinkPersistInfo(barBI) });
            }
        }
        /// <summary>
        /// 皮肤单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(e.Item.Tag.ToString());
            //e.Item.Hint = e.Item.Tag.ToString();
        }
        #endregion

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frm_DatabaseCon frm = new Frm_DatabaseCon();
            frm.ShowDialog();
        }
    }
}
